﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using Utils.DatabaseUtils;

public partial class Admin_Pages_AdminChangeInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["login"] != null)
            {
                Admin admin = (Admin)Session["login"];
                txtAddress.Text = admin.Address;
                txtDate.SelectedDate = admin.BirhDay;
                txtEmail.Text = admin.Email;
                txtMobile.Text = admin.Mobile;
                txtFullName.Text = admin.FullName;
            }
            else
            {
                Response.Redirect("../Login.aspx");
            }
            
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        AdminUI adminUI = new AdminUI();
        Admin admin = (Admin)Session["login"];
        int result = adminUI.Update(admin.Username,txtFullName.Text.Trim(),txtMobile.Text.Trim(),txtAddress.Text.Trim(),txtEmail.Text.Trim(),txtDate.SelectedDate,admin.Level);
        if (result > 0)
        {
            Response.Write("<script>alert('Update Successful');</script>");
            DataSet ds = adminUI.Get(admin.Username);
            if (TableLib.HasData(ds))
            {
                DataRow row = ds.Tables[0].Rows[0];
                Admin ae = new Admin();
                ae.Username = row["Username"].ToString().Trim();
                ae.FullName = row["FullName"].ToString();
                ae.Mobile = row["Mobile"].ToString();
                ae.Address = row["Address"].ToString();
                ae.Email = row["Email"].ToString();
                ae.BirhDay = Convert.ToDateTime(row["BirhDay"].ToString());
                ae.Level = Convert.ToByte(row["Level"].ToString());

                Session["login"] = ae;
            }
            Response.Write("<script>window.location.href='../Pages/AdminInfo.aspx';</script>");
        }
        else
        {
            Response.Write("<script>alert('Update Fail');</script>");
        }
    }
}
